﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Portal.Web.Model;
using Portal.Web.Utility;
using Portal.Exam.Bussiness;
using Portal.Core;
using Portal.Exam.Model;

namespace Portal.Exam.Render
{
    public class PrintExamResultList
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();


            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string ExamRoomId = (string)WebEnvironments.Request(SiteId, "ExamRoomId");
                if (string.IsNullOrEmpty(ExamRoomId)) throw new Exception("Tham số không hợp lệ!");
                ExamRoomCls OExamRoom = new ExamRoomBll().CreateModel(SiteId, ExamRoomId);
                if (OExamRoom==null) throw new Exception("Tham số không hợp lệ!");
                string ExamInfo = OExamRoom.RoomNo + " / " + OExamRoom.ExamSubject + " / " + OExamRoom.ExamSegmentName;

                DataTable dtExamRoomStudent = new ExamRoomStudentBll().ReadingExamRoomStudent(SiteId, ExamRoomId, null);
                if (dtExamRoomStudent.Rows.Count == 0)
                {
                    OAjaxOut.HtmlContent =
                        "  <div class=CssNoData>Không có phòng thi nào thỏa mãn điều kiện lọc</div>";
                    return OAjaxOut;
                }

                string RetHtml =
                    "   <div class=PageTitle>Số sinh viên trong phòng thi "+ExamInfo+" / tổng số có (" + dtExamRoomStudent.Rows.Count.ToString("#,##0") + ") thí sinh</div>" +
                    "   <div class=\"tabcontent\">\r\n" +
                    "   <table>" +
                    "       <tr>" +
                    "           <th class=thIndex>#</th>" +
                    "           <th>Mã SV</th>" +
                    "           <th>Tên sinh viên</th>" +
                    "           <th>Giới tính</th>" +
                    "           <th>Ngày sinh</th>" +
                    "           <th>Khóa</th>" +
                    "           <th>Lớp</th>" +
                    "           <th>Ngành</th>" +
                    "           <th>Đề</th>" +
                    "           <th>Trạng thái</th>" +
                    "           <th>Số điểm</th>" +
                    "       </tr>";

                for (int iIndex = 0; iIndex < dtExamRoomStudent.Rows.Count; iIndex++)
                {
                    string ExamRoomStudentId = (string)dtExamRoomStudent.Rows[iIndex]["ExamRoomStudentId"];
                    string StudentCode = (string)dtExamRoomStudent.Rows[iIndex]["StudentCode"];
                    string StudentName = (string)dtExamRoomStudent.Rows[iIndex]["StudentName"];
                    int Sex = (int)dtExamRoomStudent.Rows[iIndex]["Sex"];
                    int FinishExam = (int)dtExamRoomStudent.Rows[iIndex]["FinishExam"];
                    int HasGenQuestion = (int)dtExamRoomStudent.Rows[iIndex]["HasGenQuestion"];
                    DateTime StartExam = new System.DateTime();
                    if (dtExamRoomStudent.Rows[iIndex]["StartExam"] != DBNull.Value)
                    {
                        StartExam = (DateTime)dtExamRoomStudent.Rows[iIndex]["StartExam"];
                    }
                    string Status = "Chưa thi";
                    if (StartExam != new System.DateTime())
                    {
                        if (FinishExam == 0)
                        {
                            Status = "Đang thi";
                        }
                        else
                        {
                            Status = "Thi xong";
                        }
                    }

                    string sDob = "";
                    try
                    {
                        DateTime Dob = (DateTime)dtExamRoomStudent.Rows[iIndex]["Dob"];
                        sDob = Dob.ToString("dd/MM/yyyy");
                    }
                    catch { }
                    string Course = (string)dtExamRoomStudent.Rows[iIndex]["Course"];
                    string TrainingLevel = (string)dtExamRoomStudent.Rows[iIndex]["TrainingLevel"];
                    string Class = (string)dtExamRoomStudent.Rows[iIndex]["Class"];
                    int DonePoint = 0;
                    if (dtExamRoomStudent.Rows[iIndex]["DonePoint"] != DBNull.Value)
                    {
                        DonePoint = (int)dtExamRoomStudent.Rows[iIndex]["DonePoint"];
                    }
                    int TotalPoint = 0;
                    if (dtExamRoomStudent.Rows[iIndex]["TotalPoint"] != DBNull.Value)
                    {
                        TotalPoint = (int)dtExamRoomStudent.Rows[iIndex]["TotalPoint"];
                    }
                    string DonePointText = DonePoint.ToString() + "/" + TotalPoint.ToString();
                    //int FinishExam = (int)dtExamRoomStudent.Rows[iIndex]["FinishExam"];
                    if (FinishExam == 0)
                    {
                        DonePointText = "chưa có";
                    }
                    else
                    {
                        string Url = "ViewResultExamStudent.aspx?ExamRoomStudentId=" + ExamRoomStudentId;
                        DonePointText = "<a href=\"" + Url + "\">" + DonePointText + "</a>\r\n";
                    }
                    RetHtml +=
                    "       <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">" +
                    "           <td class=tdIndex>" + (iIndex + 1) + "</td>" +
                    "           <td class=tdCode>" + StudentCode + "</td>" +
                    "           <td>" + StudentName + "</td>" +
                    "           <td>" + (Sex == 1 ? "Nam" : "Nữ") + "</td>" +
                    "           <td class=tdDate>" + sDob + "</td>" +
                    "           <td>" + Course + "</td>" +
                    "           <td>" + Class + "</td>" +
                    "           <td>" + TrainingLevel + "</td>" +
                    "           <td>" + (HasGenQuestion == 1 ? "Đã tạo" : "Chưa tạo") + "</td>" +
                    "           <td>" + Status + "</td>" +
                    "           <td>" + DonePointText + "</td>" +
                    //"           <td class=tdButton><input type=button class=button value=\"Sửa\" onclick=\"javascript:ClientUpdateExamRoomStudent('" + ExamRoomStudentId + "','" + ExamRoomStudentId + "');\"></td>" +
                    //"           <td class=tdButton><input type=button class=button value=\"Xóa\" onclick=\"javascript:ClientDeleteExamRoomStudent('" + ExamRoomId + "','" + ExamRoomStudentId + "');\"></td>" +
                    //"           <td class=tdButton><input type=button class=button value=\"Session\" onclick=\"javascript:ClientSetExamRoomStudentSession('" + ExamRoomId + "','" + ExamRoomStudentId + "');\"></td>" +
                    "       </tr>"+
                    "       <th>" +
                    "       <td>" +
                    "       Ghi chú" +
                    "       </td>" +
                    "       <td colspan=9>" +
                    "       <input type=\"textbox\" id=\"txt_note" + iIndex + "\" class=\"note\"/>" +
                    "       </td>" +
                    "       </th>";
                }
                RetHtml +=
                    "   </table>\r\n" +
                    "</div>\r\n";
                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }


    }
}
